/**
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 */
package org.jiopi.ibean.loader;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jiopi.framework.core.CentralConsoleKernel;
import org.jiopi.framework.core.loader.CentralConsoleKernelLoader;
import org.jiopi.framework.exception.CentralConsoleInitializeException;
import org.jiopi.ibean.initializer.IBeanInitializer;
import org.jiopi.ibean.loader.log.LoaderLogUtil;
import org.jiopi.ibean.share.ShareUtil.ResourceUtil;

/**
 * iBean的客户端部署程序
 * 
 * @since 2010.4.10
 */
public class IBeanCenterConsoleKernelLoader implements CentralConsoleKernelLoader {
	
	public CentralConsoleKernel loadCentralConsoleKernel() {
		
		//IBean初始化
		IBeanInitializer.iBeanInitialize();
		//初始化完毕后即可确保本次运行的运行时环境的运行时目录
		
		//装载bootstrap.jar运行loadCentralConsoleKernel返回对象
		Logger loaderLogger = LoaderLogUtil.getLoaderLogger();

		ClassLoader bootstrapClassLoader = IBeanInitializer.getBootstrapClassLoader();
		
		Properties setupProperties = ResourceUtil.getPropertiesFromClassPath(bootstrapClassLoader, IBeanLoaderConstants.BOOTSTRAP_CONFIG_FILE,false);
		String bootstrapClassName = setupProperties.getProperty(IBeanLoaderConstants.IBEAN_BOOTSTRAP_LOADER_CLASS);
		Class<?> bootstrapClass=null;
		try {
			bootstrapClass = bootstrapClassLoader.loadClass(bootstrapClassName);
		} catch (ClassNotFoundException e) {
			LoaderLogUtil.logExceptionTrace(loaderLogger, Level.SEVERE, e, "iBean initialization ERROR!");
			throw new CentralConsoleInitializeException("iBean initialization ERROR!",e);
		}
		
		
		CentralConsoleKernelLoader bootstrapCentralConsoleKernelLoader=null;
		try {
			bootstrapCentralConsoleKernelLoader = (CentralConsoleKernelLoader)bootstrapClass.newInstance();
		} catch (InstantiationException e) {
			LoaderLogUtil.logExceptionTrace(loaderLogger, Level.SEVERE, e, "iBean initialization ERROR!");
			throw new CentralConsoleInitializeException(e);
		} catch (IllegalAccessException e) {
			LoaderLogUtil.logExceptionTrace(loaderLogger, Level.SEVERE, e, "iBean initialization ERROR!");
			throw new CentralConsoleInitializeException(e);
		}
		return bootstrapCentralConsoleKernelLoader.loadCentralConsoleKernel();
	}
	
	
}
